﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using BenevolentSunData;

namespace BenevolentSun
{

    public enum AlignH
    {
        Left,
        Center,
        Right
    }

    public enum AlignV
    {
        Top,
        Bottom,
        Center
    }

    public class TextField
    {

        String text;
        public String Text
        {
            get { return text; }
            set
            {
                text = value;
                refreshAlignment();
            }
        }


        Vector2 alignOrigin = Vector2.Zero;

        AlignH alignH = AlignH.Left;
        public AlignH AlignH
        {
            get { return alignH; }
            set { alignH = value; refreshAlignment(); }
        }

        AlignV alignV = AlignV.Bottom;
        public AlignV AlignV
        {
            get { return alignV; }
            set { alignV = value; refreshAlignment(); }
        }


        Vector2 textDimensions;
        public Vector2 TextDimensions
        {
            get { return textDimensions; }
        }


        private void refreshAlignment()
        {
            if (SpriteFont == null)
                return;

            textDimensions = SpriteFont.MeasureString(text);
            
            switch (alignH)
            {
                case AlignH.Left:
                    alignOrigin.X = 0;
                    break;
                case AlignH.Right:
                    alignOrigin.X = textDimensions.X;
                    break;
                case AlignH.Center:
                    alignOrigin.X = textDimensions.X / 2.0f;
                    break;
            }

            switch (alignV)
            {
                case AlignV.Bottom:
                    alignOrigin.Y = 0;
                    break;
                case AlignV.Top:
                    alignOrigin.Y = textDimensions.Y;
                    break;
                case AlignV.Center:
                    alignOrigin.Y = textDimensions.Y / 2.0f;
                    break;
            }
        }


        SpriteFont spriteFont;
        public SpriteFont SpriteFont
        {
            get { return spriteFont; }
            set { spriteFont = value; refreshAlignment(); }
        }

        public Vector2 Scale = Vector2.One;

        public Vector2 Position = Vector2.Zero;

        /// <summary>
        /// Angle of rotation in degrees around the origin.
        /// </summary>
        public float Rotation = 0f;

        /// <summary>
        /// The origin of the texture with the coordinate system top left aligned.
        /// </summary>
        public Vector2 Origin = Vector2.Zero;


        public float LayerDepth = 0f;

        public SpriteEffects SpriteEffects = SpriteEffects.None;

        public Color Color = Color.White;


        public TextField(String text, SpriteFont spriteFont)
        {
            this.text = text;
            this.spriteFont = spriteFont;

            refreshAlignment();
        }

        public TextField(String text, SpriteFont spriteFont, Color color)
        {
            this.text = text;
            this.spriteFont = spriteFont;
            this.Color = color;

            refreshAlignment();
        }

        public TextField(String text, SpriteFont spriteFont, Vector2 position, AlignH alignH, AlignV alignV)
        {
            Position.X = position.X;
            Position.Y = position.Y;
            this.text = text;
            this.spriteFont = spriteFont;
            this.alignH = alignH;
            this.alignV = alignV;

            refreshAlignment();
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            if (SpriteFont != null)
                spriteBatch.DrawString(spriteFont, text, Position, Color, Rotation, Origin + alignOrigin, Scale, SpriteEffects, LayerDepth);
        }

    }

}
